<?php
class Administrator_SeoautoController extends Zend_Controller_Action
{
	public function init() {
	}
	
	public function indexAction(){
		$objSeoauto = new HT_Model_administrator_models_seoauto();
		$do = @$this->_request->getParam('do');
		$id = (int)$this->_request->getParam('id');
		if($do == 'delete' && $id >0){
			$this->deleteSeoauto($id);
		}elseif($do == 'list'){
			$this->getListSeoauto();
		}else{
			$keyword = $this->_request->getParam('keyword');
			$this->view->keyword 	= $keyword;
		}
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/seoauto/index.js');
	}
	
	public function updateAction(){
		$objSeoauto = new HT_Model_administrator_models_seoauto();
		$do 		 = @$this->_request->getParam('do');
		$id 		= (int)$this->_request->getParam('id');
		$status 	= (int)$this->_request->getParam('status');
		//echo $do; die();
		$auto_type_id = null;
		if($do == 'submit'){
			$data = array();
			$data['auto_type_id'] 				= $this->_request->getParam('auto_type_id');
			$data['auto_content'] 				= $this->_request->getParam('auto_content');
			if($id >0){
				$return = $objSeoauto->updateData($data,$id);
			}else{
				$return = $objSeoauto->addData($data);
			}
			//$this->_redirect(WEB_PATH.'/administrator/seoauto/update?status=1&id='.$id);
			if($return > 0){
				$this->_redirect(WEB_PATH.'/administrator/seoauto');
			}else{
				$this->_redirect(WEB_PATH."/administrator/seoauto/update?status=$return&id=$id");
			}
			
		}elseif($id >0){
			$seoauto 			 = $objSeoauto->getSeoauto($id);
			$auto_type_id		= $seoauto['auto_type_id'];
			$this->view->seoauto = $seoauto;
			
		}
		
		$objUtil 			= new HT_Model_administrator_models_utility();
		$auto_type 			= $objUtil->GetCombobox('auto_type_id','auto_type_id','auto_type_name','seo_auto_type',array('isBlankVal'=>'Vui lòng chọn loại dữ liệu','defaultValue'=>$auto_type_id));
		
		$this->view->id 			= $id;
		$this->view->auto_type 		= $auto_type;
		$this->view->status 	 	= $status;
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/seoauto/update.js');
	}

	function deleteSeoauto($id){
		$objSeoauto = new HT_Model_administrator_models_seoauto();
		echo $objSeoauto->delete("auto_id=".(int)$id);die();
	}

	function getListSeoauto(){
		$objUtil 		= new HT_Model_administrator_models_utility();
		$objSeoauto 	= new HT_Model_administrator_models_seoauto();
		$keyword 		= trim($this->_request->getParam('keyword'));
		$page 			= (int)$this->_request->getParam('page');
		$size 			= PAGING_SIZE;
		if (!is_numeric($page) || $page <= 0) {
			$page = 1;
		}
		$start = $page * $size - $size;
		$totalRecord = $objSeoauto->getListSeoauto_nb(array('keyword'=>$keyword));
		$listSeoauto = $objSeoauto->getListSeoauto($start,$size,array('keyword'=>$keyword));
		$paging = trim($objUtil->paging($page, $size, $totalRecord));

		$ajaxData = null;
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		$ajaxData .= '<table cellspacing="0" class="tablesorter tablesorterBorder">';
		$ajaxData .= '<thead>';
			$ajaxData .= '<tr>';
				$ajaxData .= '<th width="15">STT</th>';
				$ajaxData .= '<th width="200" align="center">Loại nội dung</th>';
				$ajaxData .= '<th width="700" align="center">Nội dung</th>';
				$ajaxData .= '<th style="white-space: nowrap;padding-right: 5px;" align="center">Điều khiển</th>';
			$ajaxData .= '</tr>';
		$ajaxData .= '</thead>';
		
		$i=0;
		foreach($listSeoauto as $seoauto){
			$i++;
			$trClass = null;
			if($i%2 == 1) $trClass = ' class="altrow"';
			$ajaxData .= '<tr id="'.$seoauto['auto_id'].'" '.$trClass.'>';
			$ajaxData .= '<td align="center">'.$i.'</td>';
			$ajaxData .= '<td align="center">'.$seoauto['auto_type_name'].'</td>';
			$ajaxData .= '<td><a href="'.WEB_PATH.'/administrator/seoauto/update/?id='.$seoauto['auto_id'].'">'.$objUtil->tooltipString($seoauto['auto_content'],600).'</a></td>';
			$ajaxData .= '<td style="white-space: nowrap" align="center">';
			$ajaxData .= '<a href="#" onclick="deleteSeoauto('.$seoauto['auto_id'].')">Xóa</a> | <a href="'.WEB_PATH.'/administrator/seoauto/update/?id='.$seoauto['auto_id'].'">Sửa</a>';
			$ajaxData .= '</td>';
			$ajaxData .= '</tr>';
		}
		$ajaxData .= '</tbody>';
		$ajaxData .= '</table>';
		$ajaxData .= '</div>';
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		echo $ajaxData; die();
	}
}
